﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy2_13
{
    public partial class Form1 : Form
    {
        private const string SpecialChars = "!@#$%^&*()_+-=[]{}|;':\",./<>?`~";
        public Form1()
        {
            InitializeComponent();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            string password = txtPassword.Text.Trim();

            // 校验密码长度（至少8位）
            if (password.Length < 8)
            {
                MessageBox.Show("密码必须至少8位！", "提示");
                lblStrength.Text = "";
                lblStrength.ForeColor = Color.Black;
                return;
            }

            // 检测密码包含的字符类型
            bool hasDigit = false;       // 数字（0-9）
            bool hasUpper = false;       // 大写字母（A-Z）
            bool hasLower = false;       // 小写字母（a-z）
            bool hasSpecial = false;     // 特殊字符

            foreach (char c in password)
            {
                if (char.IsDigit(c))
                    hasDigit = true;
                else if (char.IsUpper(c))
                    hasUpper = true;
                else if (char.IsLower(c))
                    hasLower = true;
                else if (SpecialChars.Contains(c.ToString()))
                    hasSpecial = true;
            }

            // 统计字符类型数量
            int typeCount = 0;
            if (hasDigit) typeCount++;
            if (hasUpper) typeCount++;
            if (hasLower) typeCount++;
            if (hasSpecial) typeCount++;

            // 确定强度等级及显示样式
            string strength = "";
            Color color = Color.Black;
            switch (typeCount)
            {
                case 4:
                    strength = "强";
                    color = Color.Green;
                    break;
                case 3:
                    strength = "较强";
                    color = Color.Yellow;
                    break;
                case 2:
                    strength = "较弱";
                    color = Color.Orange;
                    break;
                case 1:
                    strength = "弱";
                    color = Color.Red;
                    break;
            }

            // 显示结果
            lblStrength.Text = strength;
            lblStrength.ForeColor = color;

        }
    }
}
